import { getCompanyID, getPosIDs, getUserDepID, getUserID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { useMemo } from 'react';
import { getBaseUrl } from '@/services/urlconst';
const baseUrl = getBaseUrl();
export const getApplyPackageServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/purchasePackController/getPackDetails/${bodyParams?.applyOrderID}`;
      return requestFunComponFiex('GET', reqUrl);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    getApplyPackageRun: request?.run,
    getApplyPackageLoading: request?.loading,
  };
};

/** 获取详情 */
export const getPurchaseSourcePackServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = { ...payload };
      let reqUrl = `${baseUrl}/purchaseSourceController/getSourceById/${bodyParams?.id}/${getUserID()}`;
      return requestFunComponFiex('GET', reqUrl);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    getPurchaseSourcePackRun: request?.run,
    getPurchaseSourcePackLoading: request?.loading,
    reloadDetailInfo: request?.refresh,
  };
};

/** 暂存接口 */
export const editPurchaseSourcePackServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        businessType: '100199031004',
        ownCompanyID: getCompanyID(),
        ownDepartmentID: getUserDepID(),
        ownPositionID: getPosIDs(),
        ownerID: getUserID(),
      };
      let reqUrl = `${baseUrl}/purchaseSourceController/editSource`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    editPurchaseSourcePackRun: request?.run,
    editPurchaseSourcePackLoading: request?.loading,
  };
};

/**
 * 查看当前流程进度
 * @controller 业务controller
 * @returns
 */
export const getImageByProcInstIDService = (callBack: (res: any) => void) => {
  const getImageByProcInstID = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/purchaseSourceController/getImageByProcInstID/${params?.procInstID}`;
      return requestFunComponFiex('GET', personUrl, {});
    },
    {
      manual: true,
      onSuccess: callBack,
    },
  );

  const data = useMemo(() => {
    return getImageByProcInstID?.data;
  }, [getImageByProcInstID?.data]);

  return {
    getImageByProcInstIDRun: getImageByProcInstID?.run,
    getImageByProcInstIDLoading: getImageByProcInstID?.loading,
    getImageByProcInstIDData: data,
  };
};

/**
 * 在线编辑
 * @param callback 接口回调
 * @returns
 */
export const saveToURLWordService = (callback: (res: any, clickType: boolean) => void) => {
  const saveToURL = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/purchaseSourceController/saveToURL`;
      // let reqUrl = `http://222.186.101.211:20103/purchaseSourceController/saveToURL`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, true);
      },
    },
  );
  return {
    wordSaveToURLRun: saveToURL?.run,
    wordSaveToURLLoading: saveToURL?.loading,
  };
};

/**招标文件审核通过之后*/
export const syncApplyStateService = (callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/purchaseApplyController/updateState`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    syncApplyStateRun: request?.run,
    syncApplyStateLoading: request?.loading,
  };
};

/**同步PDF*/
export const syncPDFtoOAServices = (cb: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let reqParams = { ...payload };
      let reqUrl = `${baseUrl}/purchaseSourceController/syncWorkFlow`;
      return requestFunComponFiex('POST', reqUrl, reqParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    syncPDFtoOARun: request?.run,
    syncPDFtoOALoading: request?.loading,
  };
};

// export { saveToURLWordService };
